<!DOCTYPE html>
<!--suppress ALL-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<!--还要引入其他静态资源就使用：th:replace="/common/common :: common_head(~{::title},~{::link})"-->
<head th:replace="/common/common :: common_head(~{::title},~{})">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>首页</title>
</head>
<body>
<h1 class="text-center">Hello</h1>

<table class="table table-hover indexTable" th:object="${initData}">
    <tr class="active">
        <td>项目编码作者</td>
        <td th:text="*{author}"></td>
    </tr>
    <tr class="success">
        <td>使用JAVA版本</td>
        <td th:text="*{since}"></td>
    </tr>
    <tr class="warning">
        <td>项目创建时间</td>
        <td th:text="*{createTime}"></td>
    </tr>
    <tr class="danger">
        <td>后端使用框架</td>
        <td th:text="*{backFrame}"></td>
    </tr>
    <tr class="info">
        <td>视图模板引擎</td>
        <td th:text="*{htmlTemp}"></td>
    </tr>
</table>
<form class="indexTable" id="formByThisPage" onsubmit="return false">
    <div class="form-group">
        <label for="exampleInputEmail1">ID:</label>
        <input type="text" class="form-control" id="userTableId"
               placeholder="请输入ID">
    </div>
    <button id="btnByThisA" type="button" class="btn btn-default">查询</button>
</form>
<table class="table table-hover indexTable">
    <tr>
        <td>查询结果</td>
        <td>
            <textarea class="form-control" rows="3" id="selectText"></textarea>
        </td>
    </tr>
</table>

<form class="indexTable" id="formByThisPage" th:action="@{/login}" method="post" onsubmit="return false">
    <div class="form-group">
        <label for="exampleInputEmail1">用户名:</label>
        <input type="text" name="username" class="form-control" id="exampleInputEmail1"
               placeholder="请输入用户名">
    </div>
    <div class="form-group">
        <label for="exampleInputPassword1">密码：</label>
        <input type="password" name="password" class="form-control" id="exampleInputPassword1" placeholder="请输入密码">
    </div>
    <button id="btnByThis" type="button" class="btn btn-default">登录</button>
</form>
<script type="text/javascript">
    $(function (ev) {
        $("#btnByThis").on('click', function () {
            window.localStorage.token = undefined;
            let sendLoginData = {username: $("#exampleInputEmail1").val(), password: $("#exampleInputPassword1").val()}
            $.ajax({
                url: contentPath + "login",
                type: "post",
                data: JSON.stringify(sendLoginData),
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (resp) {
                    if (resp.code == 200) {
                        toastr.success("登录成功!");
                        window.localStorage.token = resp.token;
                    } else {
                        toastr.error(resp.msg);
                    }

                }
            });
        })
        $("#btnByThisA").on('click', function () {
            let sendLoginData = $("#userTableId").val();
            $.ajax({
                url: contentPath + "getUserById?id=" + sendLoginData,
                type: "get",
                beforeSend: function (XMLHttpRequest) {
                    XMLHttpRequest.setRequestHeader("token", window.localStorage.token);
                },
                success: function (resp) {
                    if (resp.code == 200) {
                        $("#selectText").val(JSON.stringify(resp.data));
                        toastr.success("查询成功！！");
                    } else if (resp.code) {
                        $("#selectText").val(JSON.stringify(resp.data));
                        toastr.error(resp.msg);
                    }

                }
            });
        })
    });


</script>
</body>
</html>